/*
 * File     : AbstractFunctionPointer.java
 * Created  : 15 May 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.internal.callback;

import com.googlecode.dni.callback.FunctionPointer;
import com.googlecode.dni.library.CallingConvention;
import com.googlecode.dni.type.NativeObject;
import com.googlecode.dni.type.Pointer;

/**
 * Base class for function pointers.
 *
 * @param <T>
 *            the delegate type
 *
 * @author Matthew Wilson
 */
abstract class AbstractFunctionPointer< T > implements FunctionPointer< T >
{

    /**
     * The pointer.  This is public to help various things that want to directly
     * access the pointer without allocating a {@link Pointer} instance.
     */
    // TODO v1.1 Look at a common internal base-class
    public final long address;

    private final T delegate;

    private final CallingConvention callingConvention;


    /**
     * @param address
     *            the pointer to native code
     * @param delegate
     *            the Java delegate
     * @param callingConvention
     *            the calling convention
     */
    AbstractFunctionPointer( final long address, final T delegate, final CallingConvention callingConvention )
    {
        this.address = address;
        this.delegate = delegate;
        this.callingConvention = callingConvention;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final Pointer pointer()
    {
        return Pointer.fromAddress( this.address );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean isSameObject( final NativeObject other )
    {
        return other instanceof AbstractFunctionPointer< ? >
               && ( (AbstractFunctionPointer< ? >) other ).address == this.address;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final T getDelegate()
    {
        return this.delegate;
    }

    /** {@inheritDoc} */
    @Override
    public final CallingConvention getCallingConvention()
    {
        return this.callingConvention;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final boolean equals( final Object obj )
    {
        return obj instanceof AbstractFunctionPointer< ? >
               && ( (AbstractFunctionPointer< ? >) obj ).address == this.address;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final int hashCode()
    {
        return ( (int) this.address ) ^ ( (int) ( this.address >> 32 ) );
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final String toString()
    {
        return "FunctionPointer[0x" + Long.toHexString( this.address ) + " " + this.delegate + "]";
    }

}
